// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.iso.weather;

import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.Date;
import zombie.core.Color;
import zombie.debug.DebugLog;

public class ClimateColorInfo {
    private Color interior = new Color(0, 0, 0, 1);
    private Color exterior = new Color(0, 0, 0, 1);
    private static BufferedWriter writer;

    public ClimateColorInfo() {
    }

    public ClimateColorInfo(float r, float g, float b, float a) {
        this(r, g, b, a, r, g, b, a);
    }

    public ClimateColorInfo(float r, float g, float b, float a, float r2, float g2, float b2, float a2) {
        this.interior.r = r;
        this.interior.g = g;
        this.interior.b = b;
        this.interior.a = a;
        this.exterior.r = r2;
        this.exterior.g = g2;
        this.exterior.b = b2;
        this.exterior.a = a2;
    }

    public void setInterior(Color other) {
        this.interior.set(other);
    }

    public void setInterior(float r, float g, float b, float a) {
        this.interior.r = r;
        this.interior.g = g;
        this.interior.b = b;
        this.interior.a = a;
    }

    public Color getInterior() {
        return this.interior;
    }

    public void setExterior(Color other) {
        this.exterior.set(other);
    }

    public void setExterior(float r, float g, float b, float a) {
        this.exterior.r = r;
        this.exterior.g = g;
        this.exterior.b = b;
        this.exterior.a = a;
    }

    public Color getExterior() {
        return this.exterior;
    }

    public void setTo(ClimateColorInfo other) {
        this.interior.set(other.interior);
        this.exterior.set(other.exterior);
    }

    public ClimateColorInfo interp(ClimateColorInfo to, float t, ClimateColorInfo result) {
        this.interior.interp(to.interior, t, result.interior);
        this.exterior.interp(to.exterior, t, result.exterior);
        return result;
    }

    public void scale(float val) {
        this.interior.scale(val);
        this.exterior.scale(val);
    }

    public static ClimateColorInfo interp(ClimateColorInfo source, ClimateColorInfo target, float t, ClimateColorInfo resultColorInfo) {
        return source.interp(target, t, resultColorInfo);
    }

    public void write(ByteBuffer output) {
        output.putFloat(this.interior.r);
        output.putFloat(this.interior.g);
        output.putFloat(this.interior.b);
        output.putFloat(this.interior.a);
        output.putFloat(this.exterior.r);
        output.putFloat(this.exterior.g);
        output.putFloat(this.exterior.b);
        output.putFloat(this.exterior.a);
    }

    public void read(ByteBuffer input) {
        this.interior.r = input.getFloat();
        this.interior.g = input.getFloat();
        this.interior.b = input.getFloat();
        this.interior.a = input.getFloat();
        this.exterior.r = input.getFloat();
        this.exterior.g = input.getFloat();
        this.exterior.b = input.getFloat();
        this.exterior.a = input.getFloat();
    }

    public void save(DataOutputStream output) throws IOException {
        output.writeFloat(this.interior.r);
        output.writeFloat(this.interior.g);
        output.writeFloat(this.interior.b);
        output.writeFloat(this.interior.a);
        output.writeFloat(this.exterior.r);
        output.writeFloat(this.exterior.g);
        output.writeFloat(this.exterior.b);
        output.writeFloat(this.exterior.a);
    }

    public void load(DataInputStream input, int worldVersion) throws IOException {
        this.interior.r = input.readFloat();
        this.interior.g = input.readFloat();
        this.interior.b = input.readFloat();
        this.interior.a = input.readFloat();
        this.exterior.r = input.readFloat();
        this.exterior.g = input.readFloat();
        this.exterior.b = input.readFloat();
        this.exterior.a = input.readFloat();
    }

    public static boolean writeColorInfoConfig() {
        boolean _boolean = false;

        try {
            String string0 = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
            String string1 = "ClimateMain_" + string0;
            String string2 = zombie.ZomboidFileSystem.instance.getCacheDir() + File.separator + string1 + ".lua";
            DebugLog.log("Attempting to save color config to: " + string2);
            File file = new File(string2);

            try {
                BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file, false));

                try {
                    writer = bufferedWriter;
                    ClimateManager climateManager = ClimateManager.getInstance();
                    write("--[[");
                    write("-- Generated file (" + string1 + ")");
                    write("-- Climate color configuration");
                    write("-- File should be placed in: media/lua/server/Climate/ClimateMain.lua (remove date stamp)");
                    write("--]]");
                    bufferedWriter.newLine();
                    write("ClimateMain = {};");
                    write("ClimateMain.versionStamp = \"" + string0 + "\";");
                    bufferedWriter.newLine();
                    write("local WARM,NORMAL,CLOUDY = 0,1,2;");
                    bufferedWriter.newLine();
                    write("local SUMMER,FALL,WINTER,SPRING = 0,1,2,3;");
                    bufferedWriter.newLine();
                    write("function ClimateMain.onClimateManagerInit(_clim)");
                    byte _byte = 1;
                    write(_byte, "local c;");
                    write(_byte, "c = _clim:getColNightNoMoon();");
                    writeColor(_byte, climateManager.getColNightNoMoon());
                    bufferedWriter.newLine();
                    write(_byte, "c = _clim:getColNightMoon();");
                    writeColor(_byte, climateManager.getColNightMoon());
                    bufferedWriter.newLine();
                    write(_byte, "c = _clim:getColFog();");
                    writeColor(_byte, climateManager.getColFog());
                    bufferedWriter.newLine();
                    write(_byte, "c = _clim:getColFogLegacy();");
                    writeColor(_byte, climateManager.getColFogLegacy());
                    bufferedWriter.newLine();
                    write(_byte, "c = _clim:getColFogNew();");
                    writeColor(_byte, climateManager.getColFogNew());
                    bufferedWriter.newLine();
                    write(_byte, "c = _clim:getFogTintStorm();");
                    writeColor(_byte, climateManager.getFogTintStorm());
                    bufferedWriter.newLine();
                    write(_byte, "c = _clim:getFogTintTropical();");
                    writeColor(_byte, climateManager.getFogTintTropical());
                    bufferedWriter.newLine();
                    WeatherPeriod weatherPeriod = climateManager.getWeatherPeriod();
                    write(_byte, "local w = _clim:getWeatherPeriod();");
                    bufferedWriter.newLine();
                    write(_byte, "c = w:getCloudColorReddish();");
                    writeColor(_byte, weatherPeriod.getCloudColorReddish());
                    bufferedWriter.newLine();
                    write(_byte, "c = w:getCloudColorGreenish();");
                    writeColor(_byte, weatherPeriod.getCloudColorGreenish());
                    bufferedWriter.newLine();
                    write(_byte, "c = w:getCloudColorBlueish();");
                    writeColor(_byte, weatherPeriod.getCloudColorBlueish());
                    bufferedWriter.newLine();
                    write(_byte, "c = w:getCloudColorPurplish();");
                    writeColor(_byte, weatherPeriod.getCloudColorPurplish());
                    bufferedWriter.newLine();
                    write(_byte, "c = w:getCloudColorTropical();");
                    writeColor(_byte, weatherPeriod.getCloudColorTropical());
                    bufferedWriter.newLine();
                    write(_byte, "c = w:getCloudColorBlizzard();");
                    writeColor(_byte, weatherPeriod.getCloudColorBlizzard());
                    bufferedWriter.newLine();
                    String[] string3 = new String[]{"Dawn", "Day", "Dusk"};
                    String[] string4 = new String[]{"SUMMER", "FALL", "WINTER", "SPRING"};
                    String[] string5 = new String[]{"WARM", "NORMAL", "CLOUDY"};

                    for (int int0 = 0; int0 < 3; int0++) {
                        write(_byte, "-- ###################### " + string3[int0] + " ######################");

                        for (int int1 = 0; int1 < 4; int1++) {
                            for (int int2 = 0; int2 < 3; int2++) {
                                if (int2 == 0 || int2 == 2 || int2 == 1 && int0 == 2) {
                                    ClimateColorInfo climateColorInfo = climateManager.getSeasonColor(int0, int2, int1);
                                    writeSeasonColor(_byte, climateColorInfo, string3[int0], string4[int1], string5[int2]);
                                    bufferedWriter.newLine();
                                }
                            }
                        }
                    }

                    write("end");
                    bufferedWriter.newLine();
                    write("Events.OnClimateManagerInit.Add(ClimateMain.onClimateManagerInit);");
                    writer = null;
                    bufferedWriter.flush();
                    bufferedWriter.close();
                } catch (Throwable throwable0) {
                    try {
                        bufferedWriter.close();
                    } catch (Throwable throwable1) {
                        throwable0.addSuppressed(throwable1);
                    }

                    throw throwable0;
                }

                bufferedWriter.close();
            } catch (Exception exception0) {
                exception0.printStackTrace();
            }
        } catch (Exception exception1) {
            exception1.printStackTrace();
        }

        return _boolean;
    }

    private static void writeSeasonColor(int _int, ClimateColorInfo climateColorInfo, String string2, String string0, String string1) throws IOException {
        Color color = climateColorInfo.exterior;
        write(
            _int,
            "_clim:setSeasonColor"
                + string2
                + "("
                + string1
                + ","
                + string0
                + ","
                + color.r
                + ","
                + color.g
                + ","
                + color.b
                + ","
                + color.a
                + ",true);\t\t--exterior"
        );
        color = climateColorInfo.interior;
        write(
            _int,
            "_clim:setSeasonColor"
                + string2
                + "("
                + string1
                + ","
                + string0
                + ","
                + color.r
                + ","
                + color.g
                + ","
                + color.b
                + ","
                + color.a
                + ",false);\t\t--interior"
        );
    }

    private static void writeColor(int _int, ClimateColorInfo climateColorInfo) throws IOException {
        Color color = climateColorInfo.exterior;
        write(_int, "c:setExterior(" + color.r + "," + color.g + "," + color.b + "," + color.a + ");");
        color = climateColorInfo.interior;
        write(_int, "c:setInterior(" + color.r + "," + color.g + "," + color.b + "," + color.a + ");");
    }

    private static void write(int _int, String string1) throws IOException {
        String string0 = new String(new char[_int]).replace("\u0000", "\t");
        writer.write(string0);
        writer.write(string1);
        writer.newLine();
    }

    private static void write(String string) throws IOException {
        writer.write(string);
        writer.newLine();
    }
}
